﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;

namespace Macaw.Moss2007.ContentChangesOverviewSolution
{
    public class ObjectTypesMenuButton : ToolBarMenuButton, System.Web.UI.IPostBackEventHandler
    {
        [Flags]
        public enum enumObjectTypes
        {
            Alert = 1,
            ContentType = 2,
            Field = 4,
            File = 8,
            Folder = 16,
            Group = 32,
            Item = 64,
            List = 128,
            Navigation = 256,
            SecurityPolicy = 512,
            Site = 1024,
            User = 2048,
            View = 4096,
            Web = 8192
        }

        public ObjectTypesMenuButton()
        {
            this.ID = "ObjectTypesMenuButton";
        }

        protected override void AddMenuItems()
        {
            MenuItemTemplate miNone = this.AddMenuItem("None", "None", "/_layouts/images/EMPTY.GIF", null, null, "__doPostBack('" + this.UniqueID + "','None')");
            MenuItemTemplate miAll = this.AddMenuItem("All", "All", "/_layouts/images/EMPTY.GIF", null, null, "__doPostBack('" + this.UniqueID + "','All')");
            this.AddMenuItemSeparator();

            foreach (string ObjectTypeKey in Enum.GetNames(typeof(enumObjectTypes)))
            {
                MenuItemTemplate mi = this.AddMenuItem(ObjectTypeKey, ObjectTypeKey, "/_layouts/images/EMPTY.GIF", null, null, "__doPostBack('" + this.UniqueID + "','" + ObjectTypeKey + "')");
            }

            base.AddMenuItems();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();


            base.MenuControl.MenuFormat = MenuFormat.ArrowAlwaysVisible;
            if (!this.Page.IsPostBack)
            {
                // Select all MenuItems
                enumObjectTypes value = 0;
                foreach (string ss in Enum.GetNames(typeof(enumObjectTypes)))
                {
                    enumObjectTypes flagvalue = (enumObjectTypes)Enum.Parse(typeof(enumObjectTypes), ss);
                    value |= flagvalue;
                }
                CheckedMenuItemIds = value;
            }
            else
            {
                // Restore state
                CheckedMenuItemIds = CheckedMenuItemIds;
            }
        }

        public enumObjectTypes CheckedMenuItemIds
        {
            get
            {
                EnsureChildControls();
                return (enumObjectTypes)this.ViewState["CheckedMenuItemIds"];
            }
            set
            {
                this.ViewState["CheckedMenuItemIds"] = value;
                string sval = "";
                foreach (string s in Enum.GetNames(typeof(enumObjectTypes)))
                {
                    enumObjectTypes flagval = ((enumObjectTypes)Enum.Parse(typeof(enumObjectTypes), s));
                    if ((value & flagval) == flagval)
                    {
                        if (sval.Length != 0) sval += ",";
                        sval += s;
                    }
                }
                if (string.IsNullOrEmpty(sval))
                {
                    try
                    {
                        base.MenuControl.CheckedMenuItemIds = null;
                    }
                    catch
                    {
                        // probably an exception, because "no-items checked". This is a bug in the MenuControl. This does work.
                    }
                }
                else
                {

                    base.MenuControl.CheckedMenuItemIds = sval;
                }
            }
        }


        #region IPostBackEventHandler Members

        void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument.Equals("None"))
            {
                CheckedMenuItemIds = 0;
                return;
            }

            if (eventArgument.Equals("All"))
            {
                enumObjectTypes value = 0;
                foreach (string ss in Enum.GetNames(typeof(enumObjectTypes)))
                {
                    enumObjectTypes flagvalue = (enumObjectTypes)Enum.Parse(typeof(enumObjectTypes), ss);
                    value |= flagvalue;
                }
                CheckedMenuItemIds = value;
                return;
            }

            enumObjectTypes ea = (enumObjectTypes)Enum.Parse(typeof(enumObjectTypes), eventArgument);

            // Toggle
            CheckedMenuItemIds ^= ea;
        }

        #endregion
    }
}
